package com.cassio.stores.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cassio.common.entity.Store;
import com.cassio.stores.util.DAOConstants;
import com.cassio.stores.util.DBHelper;

public class StoreDAO {
	public static boolean persist(Store store) {
		if(store.getId() == null)
			return insert(store);
		else
			return update(store);
	}
	
	public static boolean insert(Store store) {
		SQLiteDatabase db = DBHelper.getInstance().getWritableDatabase();
		ContentValues values = new ContentValues();
		
//		values.put(Store.COL_ID , Auto generated
		values.put(Store.COL_NAME , store.getName());
		values.put(Store.COL_DESCRIPTION , store.getDescription());
		values.put(Store.COL_ADDRESS , store.getAddress());
		values.put(Store.COL_LATITUDE , store.getLatitude());
		values.put(Store.COL_LONGITUDE , store.getLongitude());
		values.put(Store.COL_CLASSIFICATION , store.getClassification());
		values.put(Store.COL_STATE_FK , store.getState().getId());
		values.put(Store.COL_CITY_FK, store.getCity().getId());
		values.put(Store.COL_PICTURE, store.getPicture());
		
		return db.insert(Store.TABLE_NAME, null, values ) != -1;
	}

	public static boolean update(Store store) {
		SQLiteDatabase db = DBHelper.getInstance().getWritableDatabase();
		ContentValues values = new ContentValues();
		
		values.put(Store.COL_NAME , store.getName());
		values.put(Store.COL_DESCRIPTION , store.getDescription());
		values.put(Store.COL_ADDRESS , store.getAddress());
		values.put(Store.COL_LATITUDE , store.getLatitude());
		values.put(Store.COL_LONGITUDE , store.getLongitude());
		values.put(Store.COL_CLASSIFICATION , store.getClassification());
		values.put(Store.COL_STATE_FK , store.getState().getId());
		values.put(Store.COL_CITY_FK, store.getCity().getId());
		values.put(Store.COL_PICTURE, store.getPicture());
		
		String[] args = {store.getId().toString()};        
        return db.update(Store.TABLE_NAME, values, Store.COL_ID  + " = ?", args) > 0;
	}

	public static boolean delete(Store store) {
		SQLiteDatabase db = DBHelper.getInstance().getWritableDatabase();
		String[] args = {store.getId().toString()};        
        return db.delete(Store.TABLE_NAME, Store.COL_ID  + " = ?", args) > 0;
	}

	public static Store find(Integer storeId) {
		SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
		
		String[] columns = {Store.COL_ID, Store.COL_NAME, Store.COL_DESCRIPTION, Store.COL_ADDRESS, Store.COL_LATITUDE, 
				Store.COL_LONGITUDE, Store.COL_CLASSIFICATION, Store.COL_STATE_FK, Store.COL_CITY_FK, Store.COL_PICTURE};
		String[] selectionArgs = {storeId.toString()};
		Cursor cursor = db.query(Store.TABLE_NAME, columns, Store.COL_ID + DAOConstants.EQUALS, selectionArgs, null, null, null);
		
		Store store = null;
		if(cursor.moveToFirst()){
			store = new Store();
			store.setId(cursor.getInt(0));
			store.setName(cursor.getString(1));
			store.setDescription(cursor.getString(2));
			store.setAddress(cursor.getString(3));
			store.setLatitude(cursor.getString(4));
			store.setLongitude(cursor.getString(5));
			store.setClassification(cursor.getInt(6));
			store.setState(StateDAO.find(cursor.getInt(7)));
			store.setCity(CityDAO.find(cursor.getInt(8)));
			store.setPicture(cursor.getBlob(9));
		}
		
		cursor.close();
		cursor.deactivate();
		db.close();
		
		return store;
	}
	
	public static List<Store> findAll() {
		SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
		String columns[] = {Store.COL_ID, Store.COL_NAME, Store.COL_DESCRIPTION, Store.COL_STATE_FK};
		Cursor cursor = db.query(Store.TABLE_NAME, columns, null, null, null, null, null);
		
		List<Store> stores = new ArrayList<Store>();
		if(cursor.moveToFirst()){
			do {
				Store store = new Store();
				store.setId(cursor.getInt(0));
				store.setName(cursor.getString(1));
				store.setDescription(cursor.getString(2));
				store.setState(StateDAO.find(cursor.getInt(3)));
				stores.add(store);
			} while (cursor.moveToNext());
		}
		
		cursor.close();
		cursor.deactivate();
		db.close();
		
		return stores;
	}
}
